<?php

namespace Home\Controller;
use Common\Controller\HomecommController;
class SellerController extends HomecommController{

	public function index(){
		
		$count = M ( "Phone" )->where(array('Issale'=>2))->count();
		$this->assign ( "count", $count );
		$link = M ( "Link" );
		$linklist =$link->select();
		$this->assign ( "linklist", $linklist );
		$this->display ();
	}

	/**
	*	预约看车
	* issale---1表示买车，0表示卖车
	**/
	public function addPhone()
	{
		$data['Id']= create_guid();//创建id
		$data['Phone']=I("post.phone");//电话号码
		$data['PStatus']=1;
		$data['IP']=get_client_ip();//获取IP地址
		$data['Wdate']=date("Y-m-d H:i:s");
		$data['Carid'] =I("post.carid",0);//车辆id
		$data['PhoneNeed'] =I("post.phoneneed");//描述
		$issale =I("post.issale");//1表示买车，0表示卖车
		if(!$issale)$issale=1;
		$data['Issale'] =$issale;
		$m= M("Phone"); 
		$c =$m->where(array('Phone'=>$data['Phone'],'PStatus'=>1))->count();
		if($c>0)
		{
			echo json_encode(array('id'=>1,'msg'=>'请不要重复提交'));
			exit;
		}
		$ips =$m->where('IP="'.$data['IP'].'" and Wdate < "'.date('Y-m-d 00:00:00').'"')->count();
		if($ips>50)
		{
			echo json_encode(array('id'=>1,'msg'=>'操作频繁，请晚些再试'));
			exit;
		}
		$result = $m->add($data);
		if($result)
		{
			echo json_encode(array('id'=>1,'msg'=>'提交成功,请保持手机畅通'));
			exit;
		}
	}


	/**
	**	砍价
	**/
	public function cutPrice()
	{
		$phone = $_POST['phone'];
		$phonecode = $_POST['phonecode'];
		if(empty($phone) || inject_check($phone)){
			echo json_encode(array("Ret" => 1,"Data" =>"", "Msg" => "手机号码错误"));exit;
		}
		//验证码
		if(!$this->checkVerify($phonecode)){
			echo json_encode(array("Ret" => 1,"Data" =>"", "Msg" => "输入验证码错误"));exit;
		}
		//查询用户是否存在
		$memberModel = D('Member');
		$rls = $memberModel->field('Id,password')->where(array('username'=>$phone))->find();
		$memberData['CusPwd'] = '';
		$memberData['CusPhone'] = $phone;
		$cusid = "";
		$cuspwd = "";
		if(count($rls) < 1)
		{
			$memberData['username'] = $phone;
			$memberData['passwpord'] = $memberModel->memberMd5($phone);
			$memberData['tel'] = $phone;
			$res = $memberModel->add($memberData);
			if(!$res)
			{
				echo json_encode(array("Ret" => 1,"Data" =>"", "Msg" => "新增用户失败！"));exit;
			}
			$cusid = $res;
			$cuspwd = $phone;
		}
		else
		{
			$cusid = $rls[0]["id"];
			$cuspwd =$rls[0]["cuspwd"];
		}
		$carid = $_POST['carid'];
		$price = $_POST['price'];
		if(!is_numeric( $price ) || $price < 0.1 || $price > 1000){
			echo json_encode(array("Ret" => 1,"Data" =>"", "Msg" => "价格错误！"));exit;
		}
		$car = M( "Cars" );
		$conditioncardt['Id'] = $carid;
		$carno = $car->where($conditioncardt)->count();
		if($carno < 1){
			echo json_encode(array("Ret" => 1,"Data" =>"", "Msg" => "无车辆"));exit;
		}
		$cutprice = M("cutprice");
		$cutpricecdt['CusId'] = $cusid;
		$cutpricecdt['CarId'] = $carid;
		$cutprice->where($cutpricecdt)->delete(); 
	
		$cutpricecdt["Id"] = create_guid();
		$cutpricecdt['CutPrice']= $price;
		$cutpricecdt['CutDate']= date('Y-m-d H:i:s',time());
		$result = $cutprice->add($cutpricecdt);
		echo json_encode(array("Ret" => 0,"Data" =>array("Id"=>$cusid,"CusPwd"=>$cuspwd), "Msg" => ""));exit;
		
				
	}
}
?>